/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * telaFornecedor.java
 *
 * Created on 19/11/2012, 21:07:18
 */
package gui;

import Fachada.Fachada;
import classes.Fornecedor;
import erro.ConexaoException;
import erro.ConsultaException;
import erro.GeralException;
import java.sql.SQLException;
import javax.swing.JOptionPane;

/**
 *
 * @author George
 */
public class telaFornecedor extends javax.swing.JFrame {
    private static Fachada f;
    String cnpj;
    String fone;
    /** Creates new form telaFornecedor */
    public telaFornecedor() {
        initComponents();
        
        this.setLocationRelativeTo(null);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroupFornec = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txtBoxNomeFornec = new javax.swing.JTextField();
        txtBoxFoneFornec = new javax.swing.JTextField();
        try{ 
            javax.swing.text.MaskFormatter tel= new javax.swing.text.MaskFormatter("(##)####-####"); 
            txtBoxFoneFornec = new javax.swing.JFormattedTextField(tel); 
        } 
        catch (Exception e){ 
        }
        jLabel4 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        ButtonSalvarFornec = new javax.swing.JButton();
        ButtonCancelFornec = new javax.swing.JButton();
        txtBoxCNPJFornec = new javax.swing.JFormattedTextField();
        try{
            javax.swing.text.MaskFormatter cnpj= new javax.swing.text.MaskFormatter("##.###.###/####-##");
            txtBoxCNPJFornec = new javax.swing.JFormattedTextField(cnpj);
        }
        catch (Exception e){
        }

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Nome:");

        jLabel2.setText("CNPJ:");

        jLabel3.setText("Fone:");

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 36));
        jLabel4.setText("Cadastro de Fornecedor");

        ButtonSalvarFornec.setText("SALVAR");
        ButtonSalvarFornec.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonSalvarFornecActionPerformed(evt);
            }
        });

        ButtonCancelFornec.setText("CANCELAR");
        ButtonCancelFornec.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonCancelFornecActionPerformed(evt);
            }
        });

        try {
            txtBoxCNPJFornec.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##.###.###/####-##")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(54, 54, 54)
                        .addComponent(jLabel4))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(29, 29, 29)
                        .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 440, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(32, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(226, Short.MAX_VALUE)
                .addComponent(ButtonSalvarFornec, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(ButtonCancelFornec, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(67, 67, 67))
            .addGroup(layout.createSequentialGroup()
                .addGap(87, 87, 87)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel2)
                        .addComponent(jLabel1))
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtBoxNomeFornec, javax.swing.GroupLayout.PREFERRED_SIZE, 280, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(txtBoxCNPJFornec, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(txtBoxFoneFornec, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 172, Short.MAX_VALUE)))
                .addContainerGap(99, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtBoxNomeFornec, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtBoxCNPJFornec, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtBoxFoneFornec, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ButtonSalvarFornec, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ButtonCancelFornec, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(40, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void ButtonSalvarFornecActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButtonSalvarFornecActionPerformed
       try{
           f = Fachada.getInstancia();
            cnpj=txtBoxCNPJFornec.getText().replace(".","");
            cnpj=cnpj.replace("-", "");
            cnpj=cnpj.replace("/", "");
            fone=txtBoxFoneFornec.getText().replace("(", "");
            fone=fone.replace(")", "");
            fone=fone.replace("-", "");
           Fornecedor fornec = new Fornecedor();
           
           fornec.setNomeFornecedor(txtBoxNomeFornec.getText());
           fornec.setCnpjFornecedor(cnpj);
           fornec.setFoneFornecedor(fone);
            try {
                f.inserirFornec(fornec);
                    limpar();
            } catch (ConsultaException ex) {
                JOptionPane.showMessageDialog(null, ex.getMessage());
            } catch (ConexaoException ex) {
              JOptionPane.showMessageDialog(null, ex.getMessage());
           
            } catch (SQLException ex) {
               JOptionPane.showMessageDialog(null, ex.getMessage());
            }
       } catch (GeralException e) {
               JOptionPane.showMessageDialog(null, e.getMessage());
       }
       
    }//GEN-LAST:event_ButtonSalvarFornecActionPerformed

    private void ButtonCancelFornecActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButtonCancelFornecActionPerformed
       telaFornecedor tlfornec = new telaFornecedor();
       int i = JOptionPane.showConfirmDialog(null,
                "Deseja realmente Cancelar?", "Cancelar", JOptionPane.YES_NO_OPTION);
        if (i == JOptionPane.YES_OPTION) {
            telaFornecedor.this.setVisible(false);
            tlfornec.dispose();
        } else {
            repaint();
        }
    }//GEN-LAST:event_ButtonCancelFornecActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(telaFornecedor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(telaFornecedor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(telaFornecedor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(telaFornecedor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new telaFornecedor().setVisible(true);
            }
        });
    }
     public void limpar() {
        txtBoxNomeFornec.setText("");
        txtBoxCNPJFornec.setValue("");
        txtBoxFoneFornec.setText("");
     }     
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton ButtonCancelFornec;
    private javax.swing.JButton ButtonSalvarFornec;
    private javax.swing.ButtonGroup buttonGroupFornec;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JFormattedTextField txtBoxCNPJFornec;
    private javax.swing.JTextField txtBoxFoneFornec;
    private javax.swing.JTextField txtBoxNomeFornec;
    // End of variables declaration//GEN-END:variables
}
